//
//  NetworkEngine.m
//  student
//
//  Created by JCai on 2018/9/4.
//  Copyright © 2018年 haiger. All rights reserved.
//

#import "NetworkEngine.h"

static NSString * const AFBaseURLString = NETWORK_URL;

@implementation NetworkEngine

+ (instancetype)sharedClient {
    static NetworkEngine *_sharedClient = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
//        NSString *afbasestr;
//        if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"IPWEB"] integerValue]==0) {
//            afbasestr = @"http://172.16.16.144:8081/";
//        }else if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"IPWEB"] integerValue]==1){
//            afbasestr = @"http://172.16.16.44:8081/";
//        }else if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"IPWEB"] integerValue]==2){
//            afbasestr = @"http://172.16.16.175:8081/";
//        }else{}
        _sharedClient = [[NetworkEngine alloc] initWithBaseURL:[NSURL URLWithString:AFBaseURLString]];
        _sharedClient.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
        AFJSONResponseSerializer *response = [AFJSONResponseSerializer serializer];
        response.removesKeysWithNullValues = YES;
        response.acceptableContentTypes = [NSSet setWithObjects:@"text/html", @"application/json", @"text/json", @"text/javascript",@"text/css", @"text/plain", @"application/x-javascript", @"application/javascript",@"application/xhtml+xml",@"application/xml", nil];
        _sharedClient.responseSerializer = response;
    });
    return _sharedClient;
}

#pragma mark ==========POST请求携带Token==========
- (void)POSTByToken:(NSString *)URLString
        data:(id)parameters
     success:(void (^)(id responseObject))success
     failure:(void (^)(NSString  *error))failure
{
    [self.requestSerializer  setValue:[UserMessage sharedInstance].model.token forHTTPHeaderField:@"Authorization"];
    [self POST:URLString parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            if ([[responseObject allKeys] containsObject:@"statusCode"]) {
                NSInteger status = [[responseObject objectForKey:@"statusCode"] integerValue];
                if (status == 200) {
                    if (success) {
                        success(responseObject);
                    }
                }else if (status == 202) {
                    return ;
                }else {
                    NSString *message = [responseObject objectForKey:@"message"];
                    if (![message isKindOfClass:[NSString class]]) {
                        message = @"";
                    }
                    if (failure) {
                        failure(message);
                    }
                }
            }
            else {
                if (success) {
                    success(responseObject);
                }
            }
        }else {
            if (failure) {
                failure(@"返回数据错误，请联系开发");
            }
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSString *message = [error localizedDescription];
        if (![message isKindOfClass:[NSString class]]) {
            message = @"";
        }
        if ([message length] <= 0) {
            message = @"";
        }
        if ([message isEqualToString:@"The Internet connection appears to be offline."]) {
            message = @"网络连接有问题，请确认后重试";
        }
        if (failure) {
            failure(message);
        }
    }];
}

#pragma mark ==========GET请求携带Token==========
- (void)GETByToken:(NSString *)URLString data:(id)parameters success:(void (^)(id))success failure:(void (^)(NSString *))failure{
    [self.requestSerializer  setValue:[UserMessage sharedInstance].model.token forHTTPHeaderField:@"Authorization"];
    [self GET:URLString parameters:parameters progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            if ([[responseObject allKeys] containsObject:@"success"]) {
                NSInteger status = [[responseObject objectForKey:@"success"] integerValue];
                if (status == 1) {
                    if (success) {
                        success(responseObject);
                    }
                }else if (status == 202) {
                    return ;
                }else {
                    NSString *message = [responseObject objectForKey:@"message"];
                    if (![message isKindOfClass:[NSString class]]) {
                        message = @"";
                    }
                    if (failure) {
                        failure(message);
                    }
                }
            }else {
                if (success) {
                    success(responseObject);
                }
            }
        }else {
            if (failure) {
                failure(@"返回数据错误，请联系开发");
            }
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSString *message = [error localizedDescription];
        if (![message isKindOfClass:[NSString class]]) {
            message = @"";
        }
        if ([message length] <= 0) {
            message = @"";
        }
        if ([message isEqualToString:@"The Internet connection appears to be offline."]) {
            message = @"网络连接有问题，请确认后重试";
        }
        if (failure) {
            failure(message);
        }
    }];
}


#pragma mark ==========POST请求==========
- (void)POST:(NSString *)URLString
        data:(id)parameters
     success:(void (^)(id responseObject))success
     failure:(void (^)(NSString  *error))failure
{
    [self POST:URLString parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            if ([[responseObject allKeys] containsObject:@"statusCode"]) {
                NSInteger status = [[responseObject objectForKey:@"statusCode"] integerValue];
                if (status == 200) {
                    if (success) {
                        success(responseObject);
                    }
                }else if (status == 202) {
                    return ;
                }else {
                    NSString *message = [responseObject objectForKey:@"message"];
                    if (![message isKindOfClass:[NSString class]]) {
                        message = @"";
                    }
                    if (failure) {
                        failure(message);
                    }
                }
            }
            else {
                if (success) {
                    success(responseObject);
                }
            }
        }else {
            if (failure) {
                failure(@"返回数据错误，请联系开发");
            }
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSString *message = [error localizedDescription];
        if (![message isKindOfClass:[NSString class]]) {
            message = @"";
        }
        if ([message length] <= 0) {
            message = @"";
        }
        if ([message isEqualToString:@"The Internet connection appears to be offline."]) {
            message = @"网络连接有问题，请确认后重试";
        }
        if (failure) {
            failure(message);
        }
    }];
}

#pragma mark ==========GET请求==========
- (void)GET:(NSString *)URLString data:(id)parameters success:(void (^)(id))success failure:(void (^)(NSString *))failure{
    [self GET:URLString parameters:parameters progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            if ([[responseObject allKeys] containsObject:@"success"]) {
                NSInteger status = [[responseObject objectForKey:@"success"] integerValue];
                if (status == 1) {
                    if (success) {
                        success(responseObject);
                    }
                }else if (status == 202) {
                    return ;
                }else {
                    NSString *message = [responseObject objectForKey:@"message"];
                    if (![message isKindOfClass:[NSString class]]) {
                        message = @"";
                    }
                    if (failure) {
                        failure(message);
                    }
                }
            }else {
                if (success) {
                    success(responseObject);
                }
            }
        }else {
            if (failure) {
                failure(@"返回数据错误，请联系开发");
            }
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSString *message = [error localizedDescription];
        if (![message isKindOfClass:[NSString class]]) {
            message = @"";
        }
        if ([message length] <= 0) {
            message = @"";
        }
        if ([message isEqualToString:@"The Internet connection appears to be offline."]) {
            message = @"网络连接有问题，请确认后重试";
        }
        if (failure) {
            failure(message);
        }
    }];
}

@end

